<?php
/**
 * Created by PhpStorm.
 * User: change
 * Date: 2016/12/26
 * Time: 9:23
 */

namespace Admin\Controller;


use Base\Controller\CurdController;
use Think\Page;

class LinkController extends CurdController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取所有数据
     * */
    protected function getAll()
    {
        if(I('get.del')){
            $map['status_id'] = 0;
        }else{
            if (!empty($_GET['status_id'])){
                $map['status_id'] = $_GET['status_id'];
            }else{
                $map['status_id'] = array('neq',0);
            }
        }
        $list = $this->model->relation($this->relation)->where($map)->order('id desc')->select();
        $this->assign('list', $list);
    }

    /**
     * 编辑界面
     * @param $id
     */
    public function showEdit($id)
    {
        if (!$id) {
            $this->error('非法操作');
        }
        $select = $this->model->relation(false)->find($id);
//        print_r($select);
        if (empty($select)) {
            $this->error('非法操作');
        }
        $this->assign('data', $select);
        $this->show('');
    }

    /**
     * 删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function del($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $this->model->where('id=' . $v)->setField('status_id', 0);
            $i++;
        }
        $this->success('删除' . $i . '条数据', $this->urlConfig['del_success']);
    }

    /**
     * 彻底删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function deleted($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            //删除图片
            $data = $this->model->find($id);
            if ($data['cover']) {
                //删除文件
                $upConfig = C('UPLOADS');
                removeFile($upConfig['PATH'] . '/' . $data['cover']);
            }
            //删除富文本文件
            if ($data['content']) {
                //删除文件
                removeContentFiles($data['content']);
            }
            $this->model->where('id=' . $v)->delete();
            $i++;
        }
        $this->success('彻底删除' . $i . '条数据', $this->urlConfig['del_success']);
    }

    /**
     * 恢复删除
     * @param string $id 要恢复的id，多个以,分割
     */
    public function recover($id){
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $this->model->where('id=' . $v)->setField('status_id', 1);
            $i++;
        }
        $this->success('恢复删除' . $i . '条数据', $this->urlConfig['del_success']);
    }
}